package com.bigzero.workflow.modules.system.controller;

import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.TableDataInfo;
import com.bigzero.workflow.common.utils.PageUtil;
import com.bigzero.workflow.modules.system.dto.RoleDto;
import com.bigzero.workflow.modules.system.entity.RoleEntity;
import com.bigzero.workflow.modules.system.query.RoleQuery;
import com.bigzero.workflow.modules.system.service.RoleService;
import com.bigzero.workflow.modules.system.vo.RoleVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理前端控制器
 * @author fanxinxiong
 * @since 2025-02-11
 */
@RestController
@RequestMapping("/system/role")
@Tag(name = "系统管理-角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperationSupport(author = "fanxinxiong", order = 1)
    @Operation(summary = "分页列表")
    @Parameters({
        //@Parameter(name = "dto", description = "部门实体", hidden = true),
        @Parameter(name = "pageNo", description = "当前页", schema = @Schema(type = "Integer"), example="1", required = true),
        @Parameter(name = "pageSize", description = "每页显示记录数", schema = @Schema(type = "Integer"), example="10", required = true)
    })
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功响应", content = @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = RoleVo.class))))
    })
    @PreAuthorize("hasAuthority('system:role:list')")
    @GetMapping("list")
    public TableDataInfo list(RoleQuery roleQuery) {
        PageUtil.startPage();
        List<RoleVo> list = roleService.queryPage(roleQuery);
        return PageUtil.getDataTable(list);
    }

    /**
     * 详情
     *
     * @param roleId 角色id
     * @return 用户详情
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "详情")
    @Parameters({
        @Parameter(name = "roleId", description = "角色id", example = "gf45e0320d93832c90dd3ed323129dd3", required = true)
    })
    @GetMapping("info/{roleId}")
    public R<RoleVo> info(@PathVariable String roleId) {
        RoleVo vo = roleService.getVoById(roleId);
        return R.ok(vo);
    }

    /**
     * 表单校验
     * @param dto
     */
    public void formValidate(RoleDto dto) {
        if(StringUtils.isBlank(dto.getCode())) {
            throw new WorkflowException("失败:角色编码不能为空");
        }
        if(StringUtils.isBlank(dto.getName())) {
            throw new WorkflowException("失败:角色名称不能为空");
        }
    }

    /**
     * 保存
     *
     * @param roleDto 数据
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('system:role:insert') or hasAuthority('system:role:update')")
    @PostMapping("save")
    public R<String> save(@RequestBody RoleDto roleDto) {
        //表单校验
        formValidate(roleDto);
        roleService.insertOrUpdate(roleDto);
        return R.ok();
    }

    @DeleteMapping("{roleId}")
    @Operation(summary = "删除")
    public void deleteRole(@PathVariable String roleId) {
        roleService.deleteRole(roleId);
    }

    @DeleteMapping()
    @Operation(summary = "批量删除")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功响应", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Void.class)))
    })
    public void deleteBatch(@RequestBody List<String> roleIds) {
        roleService.deleteBatch(roleIds);
    }

    /**
     * 角色列表
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 5)
    @Operation(summary = "角色列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功响应",
        content = @Content(
                mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = RoleEntity.class))
        ))
    })
    @GetMapping("roleList")
    public R<List<RoleEntity>> list() {
        List<RoleEntity> list = roleService.getAllEntityList();
        return R.ok(list);
    }

    /**
     * 删除
     * @param id 主键
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 4)
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('system:role:delete')")
    @DeleteMapping("delete")
    public R<String> delete(@RequestBody String id) {
        if(StringUtils.isBlank(id)) {
            throw new WorkflowException("失败:角色主键不能为空");
        }
        roleService.deleteById(id);
        return R.ok();
    }
}
